package org.quincy.rock.core.dao;

import java.util.List;
import java.util.Map;

import org.quincy.rock.core.dao.sql.Predicate;
import org.quincy.rock.core.dao.sql.Sort;
import org.quincy.rock.core.vo.PageSet;
import org.quincy.rock.core.vo.Vo;
import org.springframework.transaction.annotation.Transactional;

/**
 * <b>业务对象抽象基类。</b>
 * <p><b>详细说明：</b></p>
 * <!-- 在此添加详细说明 -->
 * 无。
 * 
 * @version 1.0
 * @author wks
 * @since 1.0
 */
public abstract class AbstractService<T extends Vo<Long>> implements IService<T> {

	/**
	 * <b>获得DAO。</b>
	 * <p><b>详细说明：</b></p>
	 * <!-- 在此添加详细说明 -->
	 * 无。
	 * @param <D>
	 * @return 数据访问对象
	 */
	protected abstract <D extends IDao<T>> D dao();

	@Override
	@Transactional
	public boolean insert(T entity, boolean ignoreNullValue, String... excluded) {
		int n = this.dao().insert(entity, ignoreNullValue, excluded);
		return n > 0;
	}

	@Override
	@Transactional
	public boolean update(T entity, boolean ignoreNullValue, Predicate where, String... excluded) {
		int n = this.dao().update(entity, ignoreNullValue, where, excluded);
		return n > 0;
	}

	@Override
	@Transactional
	public boolean updateMap(Map<String, Object> voMap, Predicate where) {
		int n = this.dao().updateMap(voMap, where);
		return n > 0;
	}

	@Override
	@Transactional
	public boolean delete(long id) {
		int n = this.dao().delete(id);
		return n > 0;
	}

	@Override
	@Transactional
	public boolean deleteMore(List<Long> ids) {
		int n = this.dao().deleteMore(ids);
		return n > 0;
	}

	@Override
	@Transactional
	public boolean deleteAll(Predicate where) {
		long n = this.dao().deleteAll(where);
		return n > 0;
	}

	@Override
	public long count(Predicate where) {
		return this.dao().count(where);
	}

	@Override
	public boolean exist(long id) {
		return this.dao().exist(id);
	}

	@Override
	public boolean existByName(String name, Object value, Long ignoreId) {
		return this.dao().existByName(name, value, ignoreId);
	}

	@Override
	public boolean existByWhere(Predicate where) {
		return this.dao().existByWhere(where);
	}

	@Override
	public T findOne(long id, String... excluded) {
		return this.dao().findOne(id, excluded);
	}

	@Override
	public List<T> findMore(List<Long> ids, String... excluded) {
		return this.dao().findMore(ids, excluded);
	}

	@Override
	public List<T> findAll(Predicate where, Sort sort, String... excluded) {
		return this.dao().findAll(where, sort, excluded);
	}

	@Override
	public T findByName(String name, Object value, String... excluded) {
		return this.dao().findByName(name, value, excluded);
	}

	@Override
	public List<T> findAllByName(String name, Object value, Sort sort, String... excluded) {
		return this.dao().findAllByName(name, value, sort, excluded);
	}

	@Override
	public PageSet<T> findPage(Predicate where, Sort sort, long pageNum, int pageSize, String... excluded) {
		return this.dao().findPage(where, sort, pageNum, pageSize, excluded);
	}

}
